using System;
using System.Collections.Generic;
using System.Text;
using EntidadesTO;
using System.Data;
using System.Data.SqlClient;

namespace AccesoDatos
{
    public class PaisDA
    {
        public List<PaisTO> ListarPais()
        {
            List<PaisTO> listaPais = new List<PaisTO>();
            SqlDataReader reader = null;
            SqlCommand comando;
            //ConexionBD instanciaConexion = ConexionBD.ObtenerInstancia();
            SqlConnection conexion_part = new SqlConnection("Data source=.\\SQLEXPRESS;Initial Catalog=RentasBD3;integrated security=true");
            conexion_part.Open();

            try
            {

                //instanciaConexion.AbrirConexion();
                comando = new SqlCommand("sp_get_SEC_Pais_all", conexion_part);
                comando.CommandType = CommandType.StoredProcedure;

                reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    PaisTO paisTO = new PaisTO();
                    paisTO.SecPais = reader.GetInt32(reader.GetOrdinal("secPais"));
                    paisTO.DesPais = reader.GetString(reader.GetOrdinal("desPais"));
                    
                    listaPais.Add(paisTO);
                }

                reader.Close();
                //instanciaConexion.CerrarConexion();
                conexion_part.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                listaPais = null;

            }
            finally
            {
                if (reader != null)
                {
                    if (reader.IsClosed == false)
                        reader.Close();
                }
                //if (instanciaConexion.ObtenerConexion().State != ConnectionState.Closed)
                if (conexion_part.State != ConnectionState.Closed)
                {
                    //instanciaConexion.CerrarConexion();
                    conexion_part.Close();
                }
                conexion_part.Dispose();
            }

            return listaPais;

        }
    }
}
